﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Dou.UI
{
    /// <summary>
    /// 按下时有缩放效果的按钮
    /// 该效果在各种游戏中都非常常见所以单独拎一个类出来实现
    /// </summary>
    public class DScaleButton : DButton, IPointerDownHandler, IPointerUpHandler
    {
        [SerializeField]
        private float _downScale = 0.96f;
        
        /// <summary>
        /// 同时需要控制缩放的外部节点，比如按钮上方的特效节点或红点节点
        /// </summary>
        [SerializeField]
        private List<Transform> _targetTransform = new List<Transform>();
        
        private Vector3 _downScaleV3 = new Vector3(0, 0, 0);

        protected override void Awake()
        {
            base.Awake();
            _downScaleV3.Set(_downScale, _downScale, _downScale);
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);
            
            transform.localScale = _downScaleV3;
            if (_targetTransform.Count > 0)
            {
                foreach (var trans in _targetTransform)
                {
                    trans.localScale = _downScaleV3;
                }
            }
        }
        
        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);
            
            transform.localScale = Vector3.one;
            if (_targetTransform.Count > 0)
            {
                foreach (var trans in _targetTransform)
                {
                    trans.localScale = Vector3.one;
                }
            }
        }
    }
}
